package com.jjb.saas.demo.app.role.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.config.client.serialnum.facade.SerialNumFacade;
import com.jjb.saas.demo.domain.role.model.RoleE;
import com.jjb.saas.demo.infrastructure.contants.CodeConstant;
import com.jjb.saas.framework.enums.enums.BooleanEnum;
import com.jjb.saas.system.client.role.request.RoleAddCmd;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author tzh * @date 2022年07月09日 16:39
 */
@AllArgsConstructor
@Component
public class RoleAddExe {
    @DubboReference
    private SerialNumFacade serialNumFacade;

    /**
     * 新增
     * @param cmd
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Response execute(RoleAddCmd cmd){
        RoleE roleE = RoleE.of();
        roleE.setRoleName(cmd.getRoleName());
        roleE.setRoleTypeEnum(cmd.getRoleTypeEnum());
        roleE.setStatusEnum(BooleanEnum.TRUE.getValue());
        roleE.setInsideEnum(BooleanEnum.FALSE.getValue());
        SingleResponse<String> response = serialNumFacade.getNextByCode(CodeConstant.ROLE_CODE);
        if(response.isSuccess()){
            roleE.setRoleCode(response.getData());
        }
        roleE.setPermsGroupIds(cmd.getPermsGroupIds());
        roleE.add();
        return Response.buildSuccess();
    }
}
